package tashie.ydrug;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ProgressBarActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	Handler handle = new Handler()	{
	@Override
   public void handleMessage(Message msg)
	{
		if(bargoing)
		{
			
		
		progressBar1.incrementProgressBy(5);
	}
	}
};
boolean isRunning = false;
	Button buttonstop;
	Button buttonrestart;
	ProgressBar progressBar1;
	Button buttonresume;
	Boolean bargoing = true;
	Boolean barrst = false;
	int SecondCounter = 0;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        barrst = false;
        
        
        buttonstop = (Button) findViewById(R.id.buttonstop);
        buttonstop.setText("Stop");
        buttonstop.setOnClickListener(this);
        
        buttonrestart = (Button) findViewById(R.id.buttonrestart);
        buttonrestart.setText("Restart");
        buttonrestart.setOnClickListener(this);
        
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        
        buttonresume = (Button) findViewById(R.id.buttonresume);
        buttonresume.setText("Resume");
        buttonresume.setOnClickListener(this);
        
        if (progressBar1.getProgress()==25)
        {
        	  AlertDialog.Builder alert = new AlertDialog.Builder(this);
              alert.setTitle("!");
              alert.setMessage("Have Fun Playing My Game");
              alert.setPositiveButton("I Will",new DialogInterface. OnClickListener(){

        			public void onClick(DialogInterface dialog, int which) {
        				// TODO Auto-generated method stub
        				Toast.makeText(getBaseContext(), "I killed a man once...",Toast.LENGTH_LONG).show();	
        			}
              	
        		
              });
        	
        }
    }
	
	public void onStart()
	{
		super.onStart();
		progressBar1.setProgress(0);	
		
		Thread back = new Thread(new Runnable()
		{
			public void run()
			{
				try
				{
				for(int i =0; i<20 && isRunning; i++)
				{
					Thread.sleep(1000);
					handle.sendMessage(handle.obtainMessage());
					if(bargoing == false)
					{
						i--;
					}
				}
				
			}
			
		catch (Throwable t)
		{
			
		}
	}
	});
isRunning = true;
back.start();
}

public void onStop()
{
	super.onStop();
	bargoing = false;
}
public void onClick(View v) {
		// TODO Auto-generated method stub
		
		if(v.equals(buttonrestart))
		{
			
			Toast.makeText(this, "Restart", Toast.LENGTH_SHORT).show();
			progressBar1.setProgress(0);
			
		}
		
	if(v.equals(buttonstop))
	{
		bargoing = false;
		Toast.makeText(this,"Paused", Toast.LENGTH_SHORT).show();
	}
		
	if(v.equals(buttonresume))
	{
		bargoing = true;
		Toast.makeText(this, "Resume", Toast.LENGTH_SHORT).show();
	}
	

}
}


